package Demo70;

/**
 * 比较含退格的字符串[栈]
 * https://leetcode.cn/problems/backspace-string-compare/
 * 使用stringBuffer来模拟栈, 来实模拟回退
 */
class Solution {
    public boolean backspaceCompare(String s, String t) {
        return changeStr(s).equals(changeStr(t));
    }

    private String changeStr(String s) {
        StringBuffer ret = new StringBuffer();
        for(int i = 0; i < s.length();i++) {
            if(ret.length() > 0 && s.charAt(i) == '#') {
                // 出栈
                ret.deleteCharAt(ret.length() - 1);
            }else {
                // 进栈
                if(s.charAt(i) != '#') ret.append(s.charAt(i));
            }
        }
        return ret.toString();
    }
}

class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.backspaceCompare("xywrrmp", "xywrrmu#p"));
    }
}